import socket

def udp_server():
    # 创建UDP socket
    server_socket = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    
    # 绑定IP和端口
    server_address = ('0.0.0.0', 12345)
    server_socket.bind(server_address)
    print(f"UDP服务器已启动，监听 {server_address}")
    
    try:
        while True:
            # 接收数据
            data, client_address = server_socket.recvfrom(1024)
            received_num = int.from_bytes(data, byteorder='little')
            print(f"收到来自 {client_address} 的数据: {received_num}")
            
            # 简单处理：将收到的数字加1后返回
            response_num = (received_num + 1) % 3
            server_socket.sendto(response_num.to_bytes(4, byteorder='little'), client_address)
            
    except KeyboardInterrupt:
        print("\n服务器正在关闭...")
    finally:
        server_socket.close()

if __name__ == '__main__':
    udp_server()